package com.aqumon.www.service.impl;

import com.aqumon.www.entity.persistent.JenkinsConfig;
import com.aqumon.www.entity.request.jenkins.ConfigCreateReq;
import com.aqumon.www.entity.request.jenkins.ConfigDeleteReq;
import com.aqumon.www.entity.request.jenkins.ConfigEditReq;
import com.aqumon.www.mapper.JenkinsConfigMapper;
import com.aqumon.www.mapper.ScriptMapper;
import com.aqumon.www.service.JenkinsConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * jenkins服务器配置 服务实现类
 * </p>
 *
 * @author aqumon
 * @since 2022-05-19
 */
@Service
public class JenkinsConfigServiceImpl extends ServiceImpl<JenkinsConfigMapper, JenkinsConfig> implements JenkinsConfigService {

    @Resource
    private JenkinsConfigMapper jenkinsConfigMapper;

    @Resource
    private ScriptMapper scriptMapper;

    @Override
    public int saveConfig(ConfigCreateReq configCreateReq) {
        JenkinsConfig jenkinsConfig = new JenkinsConfig();
        BeanUtils.copyProperties(configCreateReq, jenkinsConfig);
        jenkinsConfig.setGmtCreated(new Date());
        return jenkinsConfigMapper.insert(jenkinsConfig);
    }

    @Override
    public int editConfig(ConfigEditReq configEditReq) {
        JenkinsConfig jenkinsConfig = new JenkinsConfig();
        BeanUtils.copyProperties(configEditReq, jenkinsConfig);
        return jenkinsConfigMapper.updateByPrimaryKeySelective(jenkinsConfig);
    }

    @Override
    public String deleteConfig(ConfigDeleteReq configDeleteReq) {
        if (scriptMapper.listAllScript(configDeleteReq.getId()).size() > 0) {
            return "有脚本关联，不能删除当前配置";
        } else {
            jenkinsConfigMapper.deleteById(configDeleteReq.getId());
            return "删除成功";
        }
    }

    @Override
    public List<JenkinsConfig> listAllConfig() {
        return jenkinsConfigMapper.listAllConfig();
    }
}
